/*
 * @Author: NgSiuKei
 * @Date: 2021-06-14 16:41:15
 * @LastEditTime: 2021-06-14 17:09:31
 * @LastEditors: NgSiuKei
 * @Description: 
 * @FilePath: /三种线程同步机制/locker.h
 * 我是一个人类。你不是一个孤岛。
 */

#ifndef LOCKER_H_
#define LOCKER_H_

#include<exception>
#include<pthread.h>
#include<semaphore.h>

//封装信号量的类
class sem
{
public:
    //创建并初始化信号量
    sem()
    {
        if(sem_init(&m_sem, 0, 0) != 0)
        {
            //构造函数没有返回值，可以通过抛出异常来报告错误
            throw std::exception();
        }
    }

    //销毁信号量
    ~sem()
    {
        sem_destroy(&m_sem);
    }

    //等待信号量
    bool wait()
    {
        return sem_wait(&m_sem) == 0;
    }

    //增加信号量
    bool post()
    {
        return sem_post(&m_sem) == 0;
    }

private:
    sem_t m_sem;
};

//封装互斥锁的类
class locker
{
public:
    //创建并初始化互斥锁
    locker()
    {
        if(pthread_mutex_init(&m_mutex, nullptr) != 0)
            throw std::exception();
    }

    //销毁互斥锁
    ~locker()
    {
        pthread_mutex_destroy(&m_mutex);
    }

    //获取互斥锁
    bool lock()
    {
        return pthread_mutex_lock(&m_mutex) == 0;
    }

    //释放互斥锁
    bool unlock()
    {
        return pthread_mutex_unlock(&m_mutex) == 0;
    }

private:
    pthread_mutex_t m_mutex;
};

//封装条件变量的类
class cond
{
public:
    //创将并初始化条件变量
    cond()
    {
        if(pthread_mutex_init(&m_mutex, nullptr) != 0)
        throw std::exception();
    }

    //销毁条件变量
    ~cond()
    {
        pthread_mutex_destroy(&m_mutex);
        pthread_cond_destroy(&m_cond);
    }

    //等待条件变量
    bool wait()
    {
        int ret = 0;
        pthread_mutex_lock(&m_mutex);
        ret = pthread_cond_wait(&m_cond, &m_mutex);
        pthread_mutex_unlock(&m_mutex);
        return ret == 0;
    }

    //唤醒等待条件变量的线程
    bool signal()
    {
        return pthread_cond_signal(&m_cond) == 0;
    }

private:
    pthread_mutex_t m_mutex;
    pthread_cond_t m_cond;
};

#endif